package b.b.h;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Build.VERSION;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.widget.HeaderViewListAdapter;
import android.widget.ListAdapter;
import android.widget.PopupWindow;
import androidx.appcompat.view.menu.ListMenuItemView;
import androidx.appcompat.widget.ListPopupWindow;
import b.b.g.i.f;
import b.b.g.i.g;
import java.lang.reflect.Method;

/* compiled from: MenuPopupWindow */
public class u extends ListPopupWindow implements t {
    public static Method E;
    public t D;

    /* compiled from: MenuPopupWindow */
    public static class a extends p {
        public final int n;
        public final int o;
        public t p;
        public MenuItem q;

        public a(Context context, boolean z) {
            super(context, z);
            if (1 == context.getResources().getConfiguration().getLayoutDirection()) {
                this.n = 21;
                this.o = 22;
                return;
            }
            this.n = 22;
            this.o = 21;
        }

        public boolean onHoverEvent(MotionEvent motionEvent) {
            if (this.p != null) {
                int headersCount;
                f fVar;
                ListAdapter adapter = getAdapter();
                if (adapter instanceof HeaderViewListAdapter) {
                    HeaderViewListAdapter headerViewListAdapter = (HeaderViewListAdapter) adapter;
                    headersCount = headerViewListAdapter.getHeadersCount();
                    fVar = (f) headerViewListAdapter.getWrappedAdapter();
                } else {
                    headersCount = 0;
                    fVar = (f) adapter;
                }
                MenuItem menuItem = null;
                if (motionEvent.getAction() != 10) {
                    int pointToPosition = pointToPosition((int) motionEvent.getX(), (int) motionEvent.getY());
                    if (pointToPosition != -1) {
                        pointToPosition -= headersCount;
                        if (pointToPosition >= 0 && pointToPosition < fVar.getCount()) {
                            menuItem = fVar.getItem(pointToPosition);
                        }
                    }
                }
                MenuItem menuItem2 = this.q;
                if (menuItem2 != menuItem) {
                    g gVar = fVar.a;
                    if (menuItem2 != null) {
                        this.p.e(gVar, menuItem2);
                    }
                    this.q = menuItem;
                    if (menuItem != null) {
                        this.p.c(gVar, menuItem);
                    }
                }
            }
            return super.onHoverEvent(motionEvent);
        }

        public boolean onKeyDown(int i, KeyEvent keyEvent) {
            ListMenuItemView listMenuItemView = (ListMenuItemView) getSelectedView();
            if (listMenuItemView != null && i == this.n) {
                if (listMenuItemView.isEnabled() && listMenuItemView.getItemData().hasSubMenu()) {
                    performItemClick(listMenuItemView, getSelectedItemPosition(), getSelectedItemId());
                }
                return true;
            } else if (listMenuItemView == null || i != this.o) {
                return super.onKeyDown(i, keyEvent);
            } else {
                setSelection(-1);
                ((f) getAdapter()).a.c(false);
                return true;
            }
        }

        public void setHoverListener(t tVar) {
            this.p = tVar;
        }

        public /* bridge */ /* synthetic */ void setSelector(Drawable drawable) {
            super.setSelector(drawable);
        }
    }

    static {
        try {
            if (VERSION.SDK_INT <= 28) {
                E = PopupWindow.class.getDeclaredMethod("setTouchModal", new Class[]{Boolean.TYPE});
            }
        } catch (NoSuchMethodException unused) {
            Log.i("MenuPopupWindow", "Could not find method setTouchModal() on PopupWindow. Oh well.");
        }
    }

    public u(Context context, AttributeSet attributeSet, int i, int i2) {
        super(context, null, i, i2);
    }

    public void c(g gVar, MenuItem menuItem) {
        t tVar = this.D;
        if (tVar != null) {
            tVar.c(gVar, menuItem);
        }
    }

    public void e(g gVar, MenuItem menuItem) {
        t tVar = this.D;
        if (tVar != null) {
            tVar.e(gVar, menuItem);
        }
    }

    public p q(Context context, boolean z) {
        a aVar = new a(context, z);
        aVar.setHoverListener(this);
        return aVar;
    }
}
